import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/net/response/card_account_response.dart';

import '../a_elder/models/elder_entity.dart';
import '../net/post_request.dart';
import '../net/response/customer_family_info.dart';
import '../net/response/elder_case_list_data.dart';
import '../net/response/food/scan_old_data.dart';

class CustomerInfoModel extends BaseMvvmModel{
  @override
  void onCleared() {

  }

  Future<ScanOldData> getOldData(String scanCode) async {
    var data = await PostRequest.scanOld(scanCode);
    if('0'==data['code'].toString()){
      var old = ScanOldData.fromJson( data['data']);
      return old;
    }else{
      throw data['msg'];
    }
  }


  Future<CardAccountResponse> getCardAccount({String physicalCardNo="",String humanId = ""}) async {
    var map = {
      "physicalCardNo":physicalCardNo,
      "humanId":humanId
    };
    var data = await HttpGo.instance.postData(BookFoodApis.checkAccount,data: map,options: PostRequest.urlencodedHeader);
    if("0"==data["code"].toString()){
      var response =  CardAccountResponse.fromJson(data);
      if(response.data!=null){
        return response;
      }else{
        throw "未查询到卡信息";
      }
    }else{
      throw data['msg'];
    }
  }

  Future<List<CustomerFamilyInfo>> getCustomerFamilyInfo(String customerId) async {
    var map = {
      "customerId":customerId
    };
    var data = await HttpGo.instance.postData(HttpApi.customerFamily,data: map.toJsonMapStr(),options: PostRequest.urlencodedHeader);
    if("0"==data["code"].toString()){
      var list = ((data['data']['rows'] as List?)??[]).map((e) => CustomerFamilyInfo.fromJson(e)).toList();
      return list;
    }else{
      throw data['msg'];
    }

  }


  Future<ElderInfo> getElderInfo(dynamic customerId) async {
    var map = {
      "customerId":customerId
    };
    var data = await HttpGo.instance.postData(HttpApi.customerDetail,data: map,options: PostRequest.urlencodedHeader);
    if("0"==data["code"].toString()){
      ElderInfo model = ElderInfo.fromJson(data['data']);
      return model;
    }else{
      throw data['msg'];
    }

  }

  ///长者电子档案列表
  Future<List<ElderCaseListData>> elderCaseList({dynamic customerElderId, int page = 1,int rows = 20}) async {
    var map = {
      "customerElderId":customerElderId,
      "page":page,
      "rows":rows,
    };
    var data = await HttpGo.instance.postData(HttpApi.elderCaseDataList,data: map,options: Options(contentType: Headers.jsonContentType));
    if("0"==data['code']){
      return ((data['data']['rows'] as List)??[]).map((e) => ElderCaseListData.fromJson(e)).toList();
    }else{
      throw data['msg'];
    }
  }

  ///上传电子档案
  Future<Map> uploadElderCase({dynamic elderId, String? attachIds}) async {
    var map = {
      "elderId": elderId,
      "attachIds": attachIds,
    };
    var data = await HttpGo.instance.postData(HttpApi.uploadElderCase,
        data: map, options: Options(contentType: Headers.jsonContentType));
    if ("0" == data['code']) {
      return map;
    } else {
      throw data['msg'];
    }
  }

}